package com.example.demo.nuoqi.service.impl;

import com.example.demo.nuoqi.dao.ProblemmanagementMapper;
import com.example.demo.nuoqi.pojo.Problemmanagement;
import com.example.demo.nuoqi.service.ProblemmanagementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:Problemmanagement业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class ProblemmanagementServiceImpl implements ProblemmanagementService {

    @Autowired
    private ProblemmanagementMapper problemmanagementMapper;


    /**
     * Problemmanagement条件+分页查询
     * @param problemmanagement 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Problemmanagement> findPage(Problemmanagement problemmanagement, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(problemmanagement);
        //执行搜索
        return new PageInfo<Problemmanagement>(problemmanagementMapper.selectByExample(example));
    }

    /**
     * Problemmanagement分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Problemmanagement> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Problemmanagement>(problemmanagementMapper.selectAll());
    }

    /**
     * Problemmanagement条件查询
     * @param problemmanagement
     * @return
     */
    @Override
    public List<Problemmanagement> findList(Problemmanagement problemmanagement){
        //构建查询条件
        Example example = createExample(problemmanagement);
        //根据构建的条件查询数据
        return problemmanagementMapper.selectByExample(example);
    }


    /**
     * Problemmanagement构建查询对象
     * @param problemmanagement
     * @return
     */
    public Example createExample(Problemmanagement problemmanagement){
        Example example=new Example(Problemmanagement.class);
        Example.Criteria criteria = example.createCriteria();
        if(problemmanagement!=null){
            // 
            if(!StringUtils.isEmpty(problemmanagement.getId())){
                    criteria.andEqualTo("id",problemmanagement.getId());
            }
            // 
            if(!StringUtils.isEmpty(problemmanagement.getTitle())){
                    criteria.andLike("title","%"+problemmanagement.getTitle()+"%");
            }
            // 
            if(!StringUtils.isEmpty(problemmanagement.getContent())){
                    criteria.andEqualTo("content",problemmanagement.getContent());
            }
            // 
            if(!StringUtils.isEmpty(problemmanagement.getImg())){
                    criteria.andEqualTo("img",problemmanagement.getImg());
            }
            // 
            if(!StringUtils.isEmpty(problemmanagement.getUserName())){
                    criteria.andEqualTo("userName",problemmanagement.getUserName());
            }
            // 
            if(!StringUtils.isEmpty(problemmanagement.getUserId())){
                    criteria.andEqualTo("userId",problemmanagement.getUserId());
            }
            // 
            if(!StringUtils.isEmpty(problemmanagement.getProfessionalDirection())){
                    criteria.andEqualTo("professionalDirection",problemmanagement.getProfessionalDirection());
            }
            // 
            if(!StringUtils.isEmpty(problemmanagement.getLikeNum())){
                    criteria.andEqualTo("likeNum",problemmanagement.getLikeNum());
            }
            // 
            if(!StringUtils.isEmpty(problemmanagement.getStatus())){
                    criteria.andEqualTo("status",problemmanagement.getStatus());
            }
            // 
            if(!StringUtils.isEmpty(problemmanagement.getCreateTime())){
                    criteria.andEqualTo("createTime",problemmanagement.getCreateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        problemmanagementMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Problemmanagement
     * @param problemmanagement
     */
    @Override
    public void update(Problemmanagement problemmanagement){
        problemmanagementMapper.updateByPrimaryKey(problemmanagement);
    }

    /**
     * 增加Problemmanagement
     * @param problemmanagement
     */
    @Override
    public void add(Problemmanagement problemmanagement){
        problemmanagementMapper.insert(problemmanagement);
    }

    /**
     * 根据ID查询Problemmanagement
     * @param id
     * @return
     */
    @Override
    public Problemmanagement findById(Integer id){
        return  problemmanagementMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Problemmanagement全部数据
     * @return
     */
    @Override
    public List<Problemmanagement> findAll() {
        return problemmanagementMapper.selectAll();
    }
}
